

#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "request.pb.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"
#include <iostream>

//针对对应的请求，定义不同类型的智能指针
// typedef std::shared_ptr<muduo::Query> QueryPtr;
// typedef std::shared_ptr<muduo::Answer> AnswerPtr;


class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr; //是父类
    typedef std::shared_ptr<chen::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<chen::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<chen::AddRequest> AddRequestPtr;
    typedef std::shared_ptr<chen::AddResponse> AddResponsePtr;
    
    
    Server(int port = 8085)
        :_server(&_baseloop, muduo::net::InetAddress("0.0.0.0", port),
         "Server", muduo::net::TcpServer::kReusePort)
         ,_dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
         ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        //注册业务请求处理函数
        _dispatcher.registerMessageCallback<chen::TranslateRequest>(std::bind(&Server::onTranslate, this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 ));

        _dispatcher.registerMessageCallback<chen::AddRequest>(std::bind(&Server::onAdd, this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 ));

        
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _server.setConnectionCallback(std::bind(&Server::onConnection, this, std::placeholders::_1));        
    }

    void start()
    {
        _server.start();
        _baseloop.loop();
    }

    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected())
        {
            LOG_INFO << "新链接建立成功";
        }
        else
        {
            LOG_INFO << "新链接关闭";
        }
    }

    //未知消息的处理请求, 1 连接，2 消息，3 时间戳
    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn,
                        const MessagePtr& message,
                        muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

    std::string translate(const std::string& str)
    {
        std::unordered_map<std::string, std::string> dict_map = 
        {
            {"good", "好"}, {"gg", "没了"}, {"吃饭", "米饭"} 
        };

        auto it = dict_map.find(str);
        if(it == dict_map.end())
        {
            return "没有找到";
        }
        
        return it->second;
    }

    void onTranslate(const muduo::net::TcpConnectionPtr& conn, const TranslateRequestPtr& message, muduo::Timestamp)
    {
        //1，提取message中的有效消息，也就是需要翻译的内容
        std::string req_msg = message->msg();
        //2，进行翻译，得到结果
        std::string rsp_msg = translate(req_msg);
        //3，组织protobuf的响应
        chen::TranslateResponse resp;
        resp.set_msg(rsp_msg);
        //4，发送响应
        _codec.send(conn, resp);
    }

    void onAdd(const muduo::net::TcpConnectionPtr& conn, const AddRequestPtr& message, muduo::Timestamp)
    {
        int num1 = message->num1();
        int num2 = message->num2();
        int result = num1 + num2;

        chen::AddResponse resp;
        resp.set_result(result);
        _codec.send(conn, resp);
    }

private:
    muduo::net::TcpServer _server;  //服务器对象
    ProtobufDispatcher _dispatcher; //请求分发器对象 需要向其中注册请求处理函数
    ProtobufCodec _codec;  //protobuf协议处理器，针对收到的请求数据进行protobuf协议处理
    muduo::net::EventLoop _baseloop;
};


int main()
{
    Server server(8085);
    server.start();
    
    return 0;
}
